package com.grandbroad.eim.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.easy.west.westclient.api.base.bean.VodBean;
import com.easy.west.westclient.api.hotvod.bean.VodOrLiveBean;
import com.easy.west.westclient.api.search.bean.SearchBean;
import com.grandbroad.eim.R;
import com.grandbroad.eim.entry.LiveRoomBean;
import com.grandbroad.eim.ui.activities.LiveActivity;
import com.grandbroad.eim.ui.activities.VideoActivity;
import com.grandbroad.eim.utils.UiUtlis;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchResultAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Object> datas = new ArrayList<>();
    private String search_name;
    private String Type;
    private AppCompatActivity activity;

    public SearchResultAdapter(AppCompatActivity activity)
    {
        this.activity = activity;
    }
    public void setSearchLiveList(List<SearchBean.LiveroomDTOListBean> searchBeanList) {
        for (SearchBean.LiveroomDTOListBean liveroomDTOListBean:searchBeanList)
        {
            datas.add(liveroomDTOListBean);
        }

    }
    public void setSearchVodList(List<SearchBean.VodDTOSBean> searchBeanList) {
        for (SearchBean.VodDTOSBean vodDTOSBean:searchBeanList)
        {
            datas.add(vodDTOSBean);

        }

    }


    public void setSearch_name(String search_name) {
        this.search_name = search_name;
    }

    public void setType(String type) {
        Type = type;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

          View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_result_item, viewGroup, false);
            return new SearchResult(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        SearchResult result = (SearchResult) viewHolder;
        if (Type.equals("LIVE"))
        {
            SearchBean.LiveroomDTOListBean liveroomDTOListBean = (SearchBean.LiveroomDTOListBean) datas.get(i);
            result.tvHotTag.setText("LIVE");
            result.tvHotTag.setBackgroundResource(R.drawable.live_tag_bg);
            if (i==0)
            {
                SpannableString spannableString = new SpannableString("搜索包含"+"\""+search_name+"\""+"的直播间");
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(UiUtlis.getColor(R.color.TextFont));
                spannableString.setSpan(colorSpan, 5,6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                result.tvSearchName.setText(spannableString);
                result.tvSearchName.setVisibility(View.VISIBLE);
            }
            String title = liveroomDTOListBean.getName();
            result.tvTitle.setText(title);
            x.image().bind(result.imgHotImg,liveroomDTOListBean.getThumbnail());
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    LiveRoomBean liveRoomBean = new LiveRoomBean();
                    liveRoomBean.setChatRoomid(liveroomDTOListBean.getChatRoomid());
                    liveRoomBean.setId(liveroomDTOListBean.getId());
                    liveRoomBean.setName(liveroomDTOListBean.getName());
                    liveRoomBean.setCreated_at(liveroomDTOListBean.getCreated_at());
                    liveRoomBean.setDescript(liveroomDTOListBean.getDescript());
                    liveRoomBean.setRtmp(liveroomDTOListBean.getRtmp());
                    liveRoomBean.setRtsp(String.valueOf(liveroomDTOListBean.getRtsp()));
                    liveRoomBean.setHls(String.valueOf(liveroomDTOListBean.getHls()));
                    liveRoomBean.setStick(liveroomDTOListBean.getStick());
                    liveRoomBean.setIslive(liveroomDTOListBean.isIslive());
                    liveRoomBean.setThumbnail(liveroomDTOListBean.getThumbnail());
                    LiveRoomBean.UserBean userBean = new LiveRoomBean.UserBean();
                    userBean.setAddress(String.valueOf(liveroomDTOListBean.getUser().getAddress()));
                    userBean.setAvatar(String.valueOf(liveroomDTOListBean.getUser().getAvatar()));
                    userBean.setCity(liveroomDTOListBean.getUser().getCity());
                    userBean.setName(liveroomDTOListBean.getUser().getName());
                    userBean.setScreen_name(liveroomDTOListBean.getUser().getScreen_name());
                    userBean.setId(liveroomDTOListBean.getUser().getId());
                    userBean.setVerfied(liveroomDTOListBean.getUser().isVerfied());
                    userBean.setPhone(liveroomDTOListBean.getUser().getPhone());
                    userBean.setProvince(liveroomDTOListBean.getUser().getProvince());
                    liveRoomBean.setUser(userBean);
                    UiUtlis.startAnimActivity(activity,LiveActivity.class,v,"screen_img","bean",liveRoomBean,false);
                    /*
                    bundle.putSerializable("bean",liveRoomBean);
                    Intent intent = new Intent(viewHolder.itemView.getContext(),LiveActivity.class);
                    intent.putExtras(bundle);
                    viewHolder.itemView.getContext().startActivity(intent);
                    */

                }
            });

        }
        if (Type.equals("视频"))
        {
            SearchBean.VodDTOSBean vodDTOSBean = (SearchBean.VodDTOSBean) datas.get(i);
            result.tvHotTag.setText("REC");
            result.tvHotTag.setBackgroundResource(R.drawable.rec_tag_bg);
            if (i==0)
            {
                SpannableString spannableString = new SpannableString("搜索包含"+"\""+search_name+"\""+"的点播");
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(UiUtlis.getColor(R.color.TextFont));
                spannableString.setSpan(colorSpan, 5,6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                result.tvSearchName.setText(spannableString);
                result.tvSearchName.setVisibility(View.VISIBLE);
            }
            String title = vodDTOSBean.getName();
            result.tvTitle.setText(title);
            x.image().bind(result.imgHotImg,vodDTOSBean.getCover_img());
            //点播点击事件
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    //这里实体不一样的需要进行属性复制
                    VodBean vodBean = new VodBean();
                    vodBean.setCount(vodDTOSBean.getCount());
                    vodBean.setCover_img(vodDTOSBean.getCover_img());
                    vodBean.setCreated_at(vodDTOSBean.getCreated_at());
                    vodBean.setId(vodDTOSBean.getId());
                    vodBean.setDescrib(vodDTOSBean.getDescrib());
                    vodBean.setState(vodDTOSBean.getState());
                    vodBean.setStick(vodDTOSBean.getStick());
                    vodBean.setUrl(vodDTOSBean.getUrl());
                    vodBean.setName(vodDTOSBean.getName());
                    VodBean.UserBean userBean = new VodBean.UserBean();
                    userBean.setAddress(vodDTOSBean.getUserDTO().getAddress());
                    userBean.setAvatar(vodDTOSBean.getUserDTO().getAvatar());
                    userBean.setCity(vodDTOSBean.getUserDTO().getCity());
                    userBean.setCreated_at(vodDTOSBean.getUserDTO().getCreated_at());
                    userBean.setScreen_name(vodDTOSBean.getUserDTO().getScreen_name());
                    userBean.setProvince(vodDTOSBean.getUserDTO().getProvince());
                    userBean.setName(vodDTOSBean.getUserDTO().getName());
                    userBean.setVerfied(vodDTOSBean.getUserDTO().isVerfied());
                    userBean.setId(vodDTOSBean.getUserDTO().getId());
                    vodBean.setUser(userBean);
                    UiUtlis.startAnimActivity(activity,VideoActivity.class,v,"screen_img","bean",vodBean,false);
                    /*
                    bundle.putSerializable("bean",vodBean);
                    //点播搜索的结果前往视频播放界面
                    Intent intent = new Intent(viewHolder.itemView.getContext(),VideoActivity.class);
                    intent.putExtras(bundle);
                    viewHolder.itemView.getContext().startActivity(intent);
                    */
                }
            });
        }
    }




    @Override
    public int getItemCount() {
        return datas.size();
    }
}




    /**
     * 搜索结果视图适配器
     */
    class SearchResult extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_search_name)
        TextView tvSearchName;
        @BindView(R.id.img_hot_img)
        ImageView imgHotImg;
        @BindView(R.id.tv_hot_tag)
        TextView tvHotTag;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_user_name)
        TextView tvUserName;
        public SearchResult(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

/**
 * 第二种类型
  */
    class SearchResultViewHodler extends RecyclerView.ViewHolder
    {
        public SearchResultViewHodler(@NonNull View itemView) {
            super(itemView);
        }
    }

